<?php
namespace app\api\controller;
use app\admin\model\Activityvideo;
use app\user\model\User;
use think\Controller;
use think\Db;
use app\admin\model\Adminactivity as AdminactivityModel;
use app\admin\model\Activityalbum as ActivityalbumModel;
use app\admin\model\Banner as BannerModel;
use app\admin\model\Viplist as ViplistModel;
use app\admin\model\Adminperiphery as AdminperipheryModel;
use app\user\model\User as UserModel;
class Index extends Controller{

    //首先获取轮播图
    public function getbanner(){
        $url = config('weburl');
        $banner = BannerModel::getApiData('del=0',"sid,concat('$url',pic_url) pic,type");

        return returnJson(200,'获取成功',$banner);
    }

    //获取首页信息
    public function get_home_msg(){
            //获取上个月的
            $url = config('weburl');
            //上个月第一天呢？
            $hismap[] = ['dates','=',date('Ym', strtotime('-1 month'))];
            $retdata['history_info'] = AdminactivityModel::getInfo($hismap,'id aid,title,concat("'.$url.'",pic_url) pic,`desc`');
            $newmap[] = ['dates','=',date('Ym')];
            $retdata['new_info'] = AdminactivityModel::getInfo($newmap,'id aid,title,concat("'.$url.'",pic_url) pic,`desc`,concat(star_time,"—",end_time)bm_time');
            //接下来三个月的活动
            $nexmap[] = ['dates','>',date('Ym')];
            $retdata['new_list'] = AdminactivityModel::where($nexmap)->field('id aid,title,concat("'.$url.'",pic_url) pic')->order('solid asc,id desc')->limit(3)->select();
        return returnJson(200,'获取成功',$retdata);
    }

    //会员福利内容
    public function getvipfuli(){
        $info = ViplistModel::getInfo(['id'=>1],'content,fuli');
        return returnJson(200,'获取成功',$info);
    }

    //不同等级对应的券已
    public function getvipleve(){
        $list['l1'] = ViplistModel::getInfo('id=2','name,money,content,endmonth');
        $list['l2'] = ViplistModel::getInfo('id=3','name,money,content,endmonth');
        $list['l3'] = ViplistModel::getInfo('id=4','name,money,content,endmonth');
        return returnJson(200,'获取成功',$list);
    }


    //获取往期活动相关新
    public function getacthis(){
        $limit = $this->getlimit();
        $url = config('weburl');
        $data = input('post.');
        if(empty($data['type'])){
            return returnJson(400,'参数错误');
        }
        $map = ['dates','<',date('Ym')];
        if($data['type']>1){
            //Shangge yue
            $map = ['dates','>=',date('Ym')];
        }
        $oldmap[] = $map;
        $retdata = AdminactivityModel::where($oldmap)->field('id aid,title,concat("'.$url.'",pic_url) pic,concat(star_time,"—",end_time)bm_time,look_num,`desc`,month')->order('solid asc,id desc')->limit($limit)->select();
        $count = AdminactivityModel::where($oldmap)->count();
        return returnJson(200,'获取成功！',['totalnum'=>$count,'list'=>$retdata]);
    }

    //报名活动详情
    public function baomingact(){
        //轮播图 标题 练练了
        $data = input('post.');

        $aid = intval($data['aid']);
        $aid = 1;
        $url = config('weburl');
        $info = AdminactivityModel::getInfo(['id'=>$aid],'id aid,title,pic_top_url,`desc`,look_num,sub_num,concat(star_time,"—",end_time)bm_time,map_address,map,content');
        //轮播图呢
        if(empty($info)){
            return returnJson(400,'非法操作！');
        }
        $lunotu = explode(',',$info['pic_top_url']);
        unset($info['pic_top_url']);
        foreach ($lunotu as $k=>$v){
            $lunotu[$k] = $url.$v;
        }
        $info['pic_top_list'] = $lunotu;

        //餐饮娱乐0','酒店住宿1','露营地点2','景区门票3   type
        $info['menpiao'] = db('admin_periphery')->field('id sid,title,concat("'.$url.'",pic_url) pic,`desc`')->where("pid=$aid and is_top=1 and FIND_IN_SET('3',type)")->select();
        $info['canyin'] = db('admin_periphery')->field('id sid,title,concat("'.$url.'",pic_url) pic,`desc`')->where("pid=$aid and is_top=1 and FIND_IN_SET('0',type)")->select();
        $info['zhusu'] = db('admin_periphery')->field('id sid,title,concat("'.$url.'",pic_url) pic,`desc`')->where("pid=$aid and is_top=1 and FIND_IN_SET('1',type)")->select();
        $info['lujingdi'] = db('admin_periphery')->field('id sid,title,concat("'.$url.'",pic_url) pic,`desc`')->where("pid=$aid and is_top=1 and FIND_IN_SET('2',type)")->select();

        return returnJson(200,'获取成功！',$info);
    }

    //往期活动详情
    public function hisactdea(){
        $data = input('post.');
        $aid = intval($data['aid']);
        $aid = 1;
        $url = config('weburl');
        $info = AdminactivityModel::getInfo(['id'=>$aid],'id aid,youji');
        if(empty($info)){
            return returnJson(400,'非法操作！');
        }
        //活动视频和相册呢
         $info['album'] = ActivityalbumModel::where(['hid'=>$aid])->field("concat('$url',file_path) pic")->limit(3)->select();
         $info['video'] = Activityvideo::where(['hid'=>$aid])->field("concat('$url',file_path) pic,file_url,title")->select();
         //还有相关商家 ['pid'=>$aid,'flag'=>1,'del'=>0]
        $info['periphery']  = AdminperipheryModel::where(['pid'=>$aid,'is_top'=>1,'flag'=>1,'del'=>0])->field("id spid,title,concat('$url',pic_url)pic")->limit(6)->select();
        return returnJson(200,'获取成功！',$info);
    }

    //更多相册
    public function getalbum(){
        $limit = $this->getlimit();
        $url = config('weburl');
        $data = input('post.');
        if(empty($data['aid'])){
            return returnJson(400,'参数错误');
        }
        $aid = $data['aid'];
        $aid = 1;
        $retdata = ActivityalbumModel::where(['hid'=>$aid])->field('id aid,concat("'.$url.'",file_path) pic')->order('solid asc,id desc')->limit($limit)->select();
        $count = ActivityalbumModel::where(['hid'=>$aid])->count();

        return returnJson(200,'获取成功！',['totalnum'=>$count,'list'=>$retdata]);
    }

    //获取活动的相关商家
    public function getactshangjia(){
        $limit = $this->getlimit();
        $url = config('weburl');
        $data = input('post.');
        if(empty($data['aid'])){
            return returnJson(400,'参数错误');
        }
        $aid = $data['aid'];
        $aid = 1;
        $retdata = AdminperipheryModel::where(['pid'=>$aid,'flag'=>1,'del'=>0])->field("id spid,title,concat('$url',pic_url)pic,`desc`")->order('solid asc,id desc')->limit($limit)->select();
        $count = AdminperipheryModel::where(['pid'=>$aid,'flag'=>1,'del'=>0])->count();

        return returnJson(200,'获取成功！',['totalnum'=>$count,'list'=>$retdata]);
    }

    //景区商家详情
    public function getshangjiadetail(){
        $url = config('weburl');
        $data = input('post.');
        if(empty($data['spid'])){
            return returnJson(400,'参数错误');
        }
        $spid = $data['spid'];

        $info = AdminperipheryModel::getInfo(['id'=>$spid],"id spid,title,pic_top_url,money,map_address,map,phone,content,score_flag,`desc`");
        //图片呢
        $lunbo = array();
        foreach (explode(',',$info['pic_top_url']) as $k=>$v){
            $lunbo[] = $url.$v;
        }
        $info['pic_list'] = $lunbo;

        return returnJson(200,'获取成功！',$info);
    }

    //注册的呢
    public function register(){
        //注册呢？

    }

    //小程序授权
    public function wxlog(){
        //扫码进入 授权页面 传入
        //同时 还需要传入
        $data = input('post.');
        $openid = $data['opneid'];
        $user = UserModel::getInfo(['id'=>$openid],'id');
        if(empty($user)){
            //增加
            $user['role'] = '6';
            $user['license_plate'] = '';
            $user['openid'] = $openid;
            $user['nickname'] = rand(1111,999999);
            $user['username'] = rand(1111,999999);
            $user['create_time'] = time();
            $user['status'] = 1;
            $user['license_plate'] = '';
            User::insert($user);
        }
        $data['token']=md5($user['id'].time().rand(10000,999999));
        //查询关联表中是否有数据
        $is_plate = $user['license_plate'] == ''?'1':'0';
         return returnJson(200,'登录成功',['token'=>$data['token'],'is_plate'=>$is_plate]);
    }



    //上传单张图片
    public function upimg(){
        $res=upOneimg(0, 'img', $size = 11548484, $path = 'uploads/apifile','user');
        return json($res);
    }           

    //上传多张图片
    public function upimgs(){
        $res=upMultipleimg(0, 'img', $size = 11548484, $path = 'uploads/apifile',$module='user');
        if($res['code']==200){
            $res['imgid']=implode(',',$res['imgid']);
        }
        return json($res);
    }

    //拼接省市区数据
    public function getArea(){
        //先找到省
        $data=Db::name('china')->where('pid',0)->select();
        //再找到市
        foreach($data as $k=>$v){
            $data[$k]['city']=Db::name('china')->where('pid',$v['id'])->select();
        }
        //再找到区
        foreach($data as $k=>$v){
            foreach($v['city'] as $k1=>$v1){
                $data[$k]['city'][$k1]['area']=Db::name('china')->where('pid',$v1['id'])->select();
            }
        }
        return json($data);
    }

    //须要有个附近商家展示的呢
    public function getshangjia(){


    }


    protected function getlimit($yy=0){
        $yy = input('post.page')?input('post.page'):10;
        $page = input('page');
        $pagess = empty($page)?'1':$page;
        $limit = ($pagess-1)*$yy.','.$yy;
        return $limit;
    }


}